$(function () {
    var index
    var boolean = false
    var key
    if (localStorage["goods"]||localStorage["goods"]==[]) {//判断本地是否存在商品数据
        key = json(localStorage["goods"])//有则直接引用
    } else {//无则自建
        key[0] = {index: 0, img: "images/list/shangping1.jpg", p: "聚财兽·钱钱", small: "大“桔”大利 财源滚滚好彩头", money: 398}
        key[1] = {index: 1, img: "images/list/shangping2.jpg", p: "雪域燕麦芝士·轻享", small: "甄选OATLY燕麦露 轻享香甜浓滑", money: 298}
        key[2] = {index: 2, img: "images/list/shangping3.jpg", p: "雪域牛乳芝士", small: "经典之作 热销多年", money: 218}
        key[3] = {index: 3, img: "images/list/shangping4.jpg", p: "仙女塔", small: "情人节限定 予你甜蜜宠爱", money: 336}
        key[4] = {index: 4, img: "images/list/shangping5.bmp", p: "数字蛋糕", small: "人见人爱火爆", money: 218}
    }
    key = json(key); //转换为数组
    var user = localStorage["user"]?JSON.parse(localStorage["user"]):[]//第一次进入无数据处理
    if (user.length>0){//已有用户数据时进入
        for (var i = 0;i<user.length;i++){//遍历
            if (json(user[i]).login){//判断是否有用户处于登录状态
                localStorage["login"]=JSON.stringify(user[i])//将当前处于登录状态的账号数据分开存储
                index=i//记录账号的位置
                boolean=true//将扳机激活
                break;
            }
        }
    }
    function f(i) {//查找在goods内的位置并返回
        for (var j=0;j<key.length;j++){
            if (key[j].index==i){
                return j
            }
        }
    }
    if (boolean){//判断登录状态
        var arr=json(user[index])//取出当前用户的数据
        if (arr.listnum){//判断是否有购买记录存在
            var all = 0//用于记录是否存在物品购买记录为0，总和为0则显示购物车无商品界面
            for (var i in arr.listnum){//防止有记录无数量
                all+=parseInt(arr.listnum[i])
                if (parseInt(arr.listnum[i])>0){//添加符合要求的商品
                    $(".shop-top").html($(".shop-top").html()+`<div class="goods" style="position: relative">
            <input type="checkbox" class="select-btn" name="" zindex="${key[f(i)].index}">
            <div>
                <img src="${key[f(i)].img}" class="w-50" alt="">
                <h6>${key[f(i)].p}</h6>
                <span class="red">￥<span class="money">${key[f(i)].money}</span></span>
                <input type="button" class="num-prev d-inline-block" value="-">
                <input type="number" class="num d-inline-block position-relative" style="bottom: .2rem" value="${arr.listnum[i]}">
                <input type="button" class="num-next d-inline-block" value="+">
            </div>
        </div>`)
                }
            }
            if (all==0){
                $(".shop-none").removeClass("d-none")
            }else {
                $(".shop-none").addClass("d-none")
            }
        }else {
            $(".shop-none").removeClass("d-none")
        }
    }else {
        $(".shop-none").removeClass("d-none")
    }
})
